var _excluded = ['options', 'searchable', 'useFirstOption', 'resetWhenNotExisted', 'className'];
function _extends() {
  _extends = Object.assign
    ? Object.assign.bind()
    : function (target) {
        for (var i = 1; i < arguments.length; i++) {
          var source = arguments[i];
          for (var key in source) {
            if (Object.prototype.hasOwnProperty.call(source, key)) {
              target[key] = source[key];
            }
          }
        }
        return target;
      };
  return _extends.apply(this, arguments);
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
import { Select as AntdSelect } from 'antd';
import 'antd/es/select/style/index.js';
import classnames from 'classnames';
import React, { forwardRef, useEffect, useMemo } from 'react';
import { useFieldContext } from '../_utils/contexts/FieldContext';
import useRequestOptions from '../_utils/hooks/useRequestOptions';
import createField from '../createField';
/**
 * 下拉框
 */
var Select = /*#__PURE__*/ forwardRef(function (_ref, ref) {
  var optionsOrRequest = _ref.options,
    searchable = _ref.searchable,
    useFirstOption = _ref.useFirstOption,
    resetWhenNotExisted = _ref.resetWhenNotExisted,
    className = _ref.className,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _useFieldContext = useFieldContext(),
    label = _useFieldContext.label;
  var _useRequestOptions = useRequestOptions(optionsOrRequest),
    options = _useRequestOptions.options,
    queryOptions = _useRequestOptions.queryOptions,
    cancelQuery = _useRequestOptions.cancelQuery;
  var defaultPropsForSearch = useMemo(
    function () {
      if (searchable && typeof optionsOrRequest === 'function') {
        return {
          showSearch: true,
          onClear: function onClear() {
            cancelQuery();
            queryOptions();
          },
          onSearch: queryOptions,
          filterOption: function filterOption() {
            return true;
          },
        };
      }
      return {};
    },
    [searchable, optionsOrRequest, queryOptions, cancelQuery],
  );
  useEffect(
    function () {
      if (!useFirstOption && !resetWhenNotExisted) return;
      var existed =
        rest !== null && rest !== void 0 && rest.value && options.length
          ? options.some(function (option) {
              var _rest$value3;
              if (rest.labelInValue) {
                var _rest$value, _rest$value2;
                return rest.mode && ['multiple', 'tags'].includes(rest.mode)
                  ? (_rest$value = rest.value) === null || _rest$value === void 0
                    ? void 0
                    : _rest$value.some(function (rawValue) {
                        return (
                          (rawValue === null || rawValue === void 0 ? void 0 : rawValue.value) ===
                          option.value
                        );
                      })
                  : ((_rest$value2 = rest.value) === null || _rest$value2 === void 0
                      ? void 0
                      : _rest$value2.value) === option.value;
              }
              return rest.mode && ['multiple', 'tags'].includes(rest.mode)
                ? (_rest$value3 = rest.value) === null || _rest$value3 === void 0
                  ? void 0
                  : _rest$value3.includes(option.value)
                : rest.value === option.value;
            })
          : false;
      if (!existed) {
        // 使用首项
        if (useFirstOption && options !== null && options !== void 0 && options.length) {
          if (rest.mode && ['multiple', 'tags'].includes(rest.mode)) {
            var _rest$onChange;
            (_rest$onChange = rest.onChange) === null || _rest$onChange === void 0
              ? void 0
              : _rest$onChange.call(
                  rest,
                  rest.labelInValue ? [options[0]] : [options[0].value],
                  //@ts-ignore
                  [options[0]],
                );
          } else {
            var _rest$onChange2;
            (_rest$onChange2 = rest.onChange) === null || _rest$onChange2 === void 0
              ? void 0
              : _rest$onChange2.call(
                  rest,
                  rest.labelInValue ? options[0] : options[0].value,
                  //@ts-ignore
                  options[0],
                );
          }
          return;
        }

        // 重置为 undefined
        if (resetWhenNotExisted && rest !== null && rest !== void 0 && rest.value) {
          if (rest.mode && ['multiple', 'tags'].includes(rest.mode)) {
            var _rest$onChange3;
            (_rest$onChange3 = rest.onChange) === null || _rest$onChange3 === void 0
              ? void 0
              : _rest$onChange3.call(rest, [], []);
          } else {
            var _rest$onChange4;
            // @ts-ignore
            (_rest$onChange4 = rest.onChange) === null || _rest$onChange4 === void 0
              ? void 0
              : _rest$onChange4.call(rest, undefined, undefined);
          }
        }
      }
    },
    [options],
  );
  return /*#__PURE__*/ React.createElement(
    AntdSelect,
    _extends(
      {
        placeholder: '\u8BF7\u9009\u62E9'.concat(label),
      },
      defaultPropsForSearch,
      {
        className: classnames('szzj-select', className),
      },
      rest,
      {
        options: options,
        ref: ref,
      },
    ),
  );
});
export default createField(Select);
